package com.ihr360.payroll.controller.payslipconfig;

import com.ihr360.authority.sharing.annonation.RestControl;
import com.ihr360.authority.sharing.privilege.AccessControlMethod;
import com.ihr360.commons.entity.AuthUserInfo;
import com.ihr360.commons.utils.RestToReturnUtils;
import com.ihr360.maven.plugin.ModuleCodeStore;
import com.ihr360.maven.plugin.annotation.FunctionDefinition;
import com.ihr360.maven.plugin.annotation.ModuleDefinition;
import com.ihr360.payroll.config.PayslipConfigEntity;
import com.ihr360.payroll.model.dto.salary.plan.config.PayslipConfig;
import com.ihr360.payroll.service.payslip.PayslipConfigService;
import com.ihr360.rest.service.SimpleRestService;
import com.ihr360.rest.webmvc.AbstractRestSimpleWithLongIdController;
import com.ihr360.validation.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author tomtao
 */
@RestController
@RequestMapping("/payslipConfigs")
@ModuleDefinition(mainModuleCode = ModuleCodeStore.Module.SALARY_CODE,
        mainModuleName = ModuleCodeStore.Module.SALARY_NAME,
        subModuleCode = ModuleCodeStore.Module.Salary.PAY_SLIP_CODE,
        subModuleName = ModuleCodeStore.Module.Salary.PAY_SLIP_NAME)
@RestControl(controls = {
        AccessControlMethod.METHODS.batchDelete,
        AccessControlMethod.METHODS.deleteOne,
        AccessControlMethod.METHODS.create,
        AccessControlMethod.METHODS.getAll,
        AccessControlMethod.METHODS.getAllInCompany,
        AccessControlMethod.METHODS.update})
public class PayslipConfigController extends AbstractRestSimpleWithLongIdController<PayslipConfig>{

    @Autowired
    PayslipConfigService payslipConfigService;

    @Override
    public SimpleRestService<PayslipConfig, Long> getSimpleRestService() {
        return payslipConfigService;
    }

    @FunctionDefinition(functionName = "保存工资单", functionCode = ModuleCodeStore.Function.PAYSLIP_CONFIG)
    @PostMapping(value = {"/save/{salaryPlanId}", "/update/{salaryPlanId}"}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public boolean saveOrUpdate(@PathVariable Long salaryPlanId, @RequestBody PayslipConfig payslipConfig, AuthUserInfo authUserInfo) {
        if (payslipConfigService.isDuplicateName(payslipConfig.getName(), payslipConfig.getId())) {
            ValidationUtils.reject("", "名称不能重复！");
        }
        payslipConfig.setCompanyId(authUserInfo.getCompanyId());
        payslipConfigService.saveOrUpdate(payslipConfig, salaryPlanId);
        return true;
    }

    @FunctionDefinition(functionName = "删除工资单", functionCode = ModuleCodeStore.Function.PAYSLIP_CONFIG)
    @DeleteMapping(value = "/deletePayslipConfig", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void deletePayslipConfig(@RequestParam Long salaryPlanId, @RequestParam Long payslipConfigId) {
        payslipConfigService.deletePayslipConfig(salaryPlanId, payslipConfigId);
        RestToReturnUtils.setMessage("删除成功");
    }

    @FunctionDefinition(functionName = "获取工资单列表", functionCode = ModuleCodeStore.Function.PAYSLIP_CONFIG)
    @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<PayslipConfigEntity> getList(AuthUserInfo authUserInfo) {
        return payslipConfigService.getList(authUserInfo.getCompanyId());
    }

    @FunctionDefinition(functionName = "获取方案下的所有工资单", functionCode = ModuleCodeStore.Function.PAYSLIP_CONFIG)
    @GetMapping(value = "/list/{salaryPlanId}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity getListBySalaryPlanId(@PathVariable Long salaryPlanId, @RequestParam String projection) {
        List<PayslipConfig> payslipConfigList = payslipConfigService.getListBySalaryPlanId(salaryPlanId);
        return super.toResource(payslipConfigList, projection);
    }
}
